package yl.hs.bmipfull.service.salemanage.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.laboratory.IProductCateDao;
import yl.hs.bmipfull.daos.laboratory.IProductInfoDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractProductsDao;
import yl.hs.bmipfull.pojo.salemanage.SaleContractEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleContractProductsEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleContractProductsTemp;
import yl.hs.bmipfull.service.salemanage.IContractPriceAdjustmentService;
import yl.hs.bmipfull.service.salemanage.ISaleContractProductsService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.salemanmage.SaleContractProductsModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  销售合同产品信息服务层接口实现<br/>
 * 创建时间: 2021年05月19日 15时41分18秒 <br/>
 *
 * @author 谢东
 */
@Service
public class SaleContractProductsServiceImpl extends SaleBaseService implements ISaleContractProductsService {

    @Autowired
    ISaleContractProductsDao dao;

    @Autowired
    IContractPriceAdjustmentService adjustmentService;
    @Autowired
    IProductCateDao productCateDao;
    @Autowired
    IProductInfoDao productInfoDao;

    @Autowired
    ISaleContractDao saleContractDao;

    @Override
    public List<Map<String,Object>> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(SaleContractProductsEntity.class);
        List<Map<String,Object>> result=new ArrayList<>();
        List<SaleContractProductsTemp> contractProducts = dao.queryListAndNowPrice(pageQuery.getQuery());
        var contractSNList =contractProducts.stream().map(t->t.getContractSN()).distinct().toList();
        if(contractSNList.size()>0) {
            List<SaleContractEntity> contractList = saleContractDao.queryBylistSn(contractSNList);
            contractProducts.forEach(t -> {
//            float productPrice = adjustmentService.getProductPrice(t.getSn(), new Date());
//            if (productPrice != 0.00f) {
//                t.setPrice(productPrice);
//            }
                var contractOpt = contractList.stream().filter(c -> c.getSn().equals(t.getContractSN())).findFirst();

                var customerName = "";
                var map = t.toMap();
                if (contractOpt.isPresent()) {
                    customerName = contractOpt.get().getCustomerName();
                }
                map.put("customerName", customerName);
                result.add(map);
            });
        }
        return result ;
    }

    @Override
    public PageResult<SaleContractProductsEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(SaleContractProductsEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<SaleContractProductsEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public SaleContractProductsEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveSaleContractProducts(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        if (Tl.isEmpty(sn)) {
            //执行保存
            SaleContractProductsEntity entity = new SaleContractProductsEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(SaleContractProductsEntity.class));

            if(dao.queryByProductSn(entity.getProductSN(),entity.getContractSN()).size()>0){
                return -1;
            };
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入销售合同产品信息信息", Tl.toJson(entity), SaleContractProductsEntity.class);
            }
        } else {
            //执行修改
            var entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, SaleContractProductsEntity.class);
            }
        }

        return result;
    }

    @Override
    public Integer saveSaleProducts(SaleContractProductsModel data) throws Exception {
        var result = 0;
        var results = data.getData();
        if(results!=null && results.size()>0){
            for (SaleContractProductsEntity saleContractProductsEntity : results) {
                SaleContractProductsEntity entity = saleContractProductsEntity;
                var productinfo = productInfoDao.findBySN(entity.getProductSN());
                if(productinfo!=null){
                    entity.setProductName(productinfo.getProductName());
                    entity.setProductSpec(productinfo.getProductSpec());
                }
                entity.setSn(queryNewSN(SaleContractProductsEntity.class));
                if(dao.queryByProductSn(entity.getProductSN(),entity.getContractSN()).size()>0){
                }else{
                    result += dao.insertNew(entity);
                }
                if (result > 0) {
                    writeLog(ActionType.CREATE, entity.getSn(), "插入销售合同产品信息信息", Tl.toJson(entity), SaleContractProductsEntity.class);
                }
            }

        }
        return result;
    }

    @Override
    public Integer delete(String[] sn) {
        var result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, SaleContractProductsEntity.class);
        }
        return result;
    }

    @Override
    public List<String> querySaleByProductSn(String sn) {
        return dao.querySaleByProductSn(sn);
    }

    @Override
    public List<SaleContractProductsEntity> queryBySaleBSn(String sn) {
        return dao.queryBySaleBSn(sn);
    }

    @Override
    public List<SaleContractProductsEntity> queryByProductSn(String sn,String saleSN) {
        return dao.queryByProductSn(sn,saleSN);
    }

    @Override
    public List<SaleContractProductsEntity> queryByContractSNAndCateSN(String sn, String productCateSN) {
        return dao.queryByContractSNAndCateSN(sn,productCateSN);
    }

    @Override
    public List<Map<String, Object>> getSaleContractProductCateName(String sn) {
        var productList =  dao.queryBySaleBSn(sn);
        List<Map<String, Object>> result =new  ArrayList<>();
        productList.forEach(t->{
            var productCate = productCateDao.findBySN(t.getProductCate());
            var entity = t.toMap();
            entity.put("productCateName",productCate.getCateName());
            result.add(entity);
        });
        return result;
    }
}